#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n, x1, y1, x2, y2;
		scanf("%d%d%d%d%d", &n, &x1, &y1, &x2, &y2);
		int t1, t2;
		t1 = min({ x1,y1,n - x1 + 1,n - y1 + 1 });
		t2 = min({ x2,y2,n - x2 + 1,n - y2 + 1 });
		printf("%d\n", abs(t1 - t2));
	}
	return 0;
}